//
//  NSString+Regex.m
//  CTalk
//
//  Created by 郭世清 on 16/6/29.
//  Copyright © 2016年 Chinac.com. All rights reserved.
//

#import "NSString+Regex.h"

@implementation NSString (Regex)


/**
 *  检查手机号码是否输入正确
 *
 *  @return true或false
 */
-(BOOL)checkPhoneNumInput
{
    
//    NSString * str = @"^((13[0-9])|(147)|(145)|(15[^4])|(18[0-9])|(17[0-9]))\\d{8}$";
    NSString * str = @"^((13[0-9])|(147)|(145)|(15[0-3,5-9])|(18[0-9])|(17[0-9]))\\d{8}$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}

- (BOOL)checkISEightNum
{
//    NSString * str = @"^1[0-9]{7}$";
    NSString *str = @"^(0[0-9]{2,3})?([2-9][0-9]{6,7})+([0-9]{1,4})?$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regextestStr evaluateWithObject:self];
}

/**
 *  登录手机号码的验证
 *  包括 8位的或者是11位的
 *
 *  @return true或false
 */
- (BOOL)checkLoginPhoneNum
{
    return ([self checkPhoneNumInput] || [self checkISEightNum]);
}

/**
 *  检查邮箱是否输入正确
 *
 *  @return true或false
 *  @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
 */
-(BOOL)checkEmailInput
{
    NSString *str = @"[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}


/**
 @brief 验证是否数字和英文字母组成
 */
- (BOOL)checkIsNumberAndCharacter{
    
    NSString * str = @"^[A-Za-z0-9]+$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
    
}

/**
 *  检查是不是中英文组成
 *
 *  @return true或false
 */
- (BOOL)checkChineseAndEnglish
{
    NSString * str = @"^[A-Za-z\u4e00-\u9fa5]+$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    
    for (NSUInteger i = 0; i < self.length; ++i) {
        if (![regextestStr evaluateWithObject:[self substringWithRange:NSMakeRange(i, 1)]]) {
            return NO;
        }
    }
    return YES;
}

/**
 *  验证是中英文数字组成
 *
 *  @return true或false
 */
- (BOOL)checkChineseAndEnglishaAndDigital
{
    NSString * str = @"^[A-Za-z0-9\u4e00-\u9fa5]+$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}

//0.01-99999999
- (BOOL)checkIsDecimals
{
    NSString * str = @"^0\\.([1-9]|\\d[1-9])$|^[1-9]\\d{0,8}\\.\\d{0,2}$|^[1-9]\\d{0,8}$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}

/**
 *
 * 验证座机
 *
 *  @return  true或false
 */
- (BOOL)checkFixPhone
{
    NSString * str = @"^[0][1-9]{2,3}[0-9]{5,10}$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    
    NSString * str1 = @"^[1-9]{1}[0-9]{5,8}$";
    NSPredicate *regextestStr1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str1];
    
    return ([regextestStr evaluateWithObject:self] || [regextestStr1 evaluateWithObject:self]);
}

/**
 *  检查是否是中文
 *
 *  @return 包含true 或 不包含false
 */
- (BOOL)checkISChinese{
    NSString *str = @"^[\u4E00-\u9FA5]*$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    for (NSUInteger i = 0; i < self.length; ++i) {
        if ([regextestStr evaluateWithObject:[self substringWithRange:NSMakeRange(i, 1)]]) {
            return YES;
        }
    }
    return NO;
}

/**
 *  验证是姓名组成 ：汉字、字母、数字、空格、英文逗号和句号
 *
 *  @return true或false
 */
- (BOOL)checkFullName
{
    NSString * str = @"^[A-Za-z0-9\u4e00-\u9fa5 \\,\\.]+$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}

/**
 *  验证员工姓名：姓名支持输入：汉字、字母、数字、空格、英文逗号和句号，但不支持表情符号，且只能以汉字、字母或数字开头
 *
 *  @return
 */
- (BOOL)validateStaffName
{
    NSString * str = @"^[\\u4e00-\\u9fa5a-zA-Z\\d]*[A-Za-z0-9\\u4e00-\\u9fa5 \\\\,\\\\.]+$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}

//判断是否以汉字、字母或数字开头
- (BOOL)validateFirstLetter
{
    NSString *pattern = @"^[\u4e00-\u9fa5A-Za-z0-9]";
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: 0 error: &error];
    NSArray *match = [regex matchesInString: self options: 0 range: NSMakeRange(0, [self length])];
    if (match.count != 0) {
        return YES;
    }
    
    return NO;
}

//判断企业帐号是否以字母或数字开头
- (BOOL)validateUserAccount
{
    NSString *pattern = @"^[a-zA-Z0-9][a-zA-Z0-9_]{1,20}$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    return  [regextestStr evaluateWithObject:self];
    
//    NSError *error = NULL;
//    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: 0 error: &error];
//    NSArray *match = [regex matchesInString: self options: 0 range: NSMakeRange(0, [self length])];
//    if (match.count != 0) {
//        return YES;
//    }
//    
//    return NO;
}


- (BOOL)checkContainsEmoji
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray *match = [regex matchesInString: self options: 0 range: NSMakeRange(0, [self length])];
    if (match.count != 0) {
        return YES;
    }
    
    return NO;
}

/**
 *  删除空格
 *
 *  @return 删除空格后的字符串
 */
- (NSString*)deleteSpace{
    NSString *trimmedString = [self stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (trimmedString) {
        return trimmedString;
    }else{
        return nil;
    }
}


/**
 *  检查是不是数字
 *
 *  @return true或 false
 */
- (BOOL)checkIsNumber
{
    NSString * str = @"^[0-9]*$";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}

/**
 *  检查是不是数字或者小数
 *
 *  @return true或false
 */
- (BOOL)checkIsNumberorDecimals
{
    NSString * str = @"[0-9]+(.[0-9]+)?";
    NSPredicate *regextestStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return  [regextestStr evaluateWithObject:self];
}

/**
 *  根据表达式获取字符串
 *
 *  @param regex 正则表达式
 *
 *  @return 返回匹配的字符串
 */
- (NSArray *)findStringByRegex:(NSString *)regex {
    
    //2.创建正则表达式实现对象
    NSRegularExpression *expression = [[NSRegularExpression alloc] initWithPattern:regex options:NSRegularExpressionCaseInsensitive error:nil];
    
    //3. expression  查找符合正则表达式的字符串
    NSArray *items = [expression matchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, self.length)];
    
    NSMutableArray *itemArray = [NSMutableArray array];
    //4.循环遍历查找出来的结果
    for (NSTextCheckingResult *result in items) {
        
        //符合表达的字符串的范围
        NSRange range = [result range];
        
        NSString *matchString = [self substringWithRange:range];
        [itemArray addObject:matchString];
    }
    
    return itemArray;
}

- (NSArray*)componentsSeparatedByRegex2:(NSString *)pattern
{
    NSUInteger pos = 0;
    NSRange area = NSMakeRange(0, [self length]);
    
    NSRegularExpression *regEx = [NSRegularExpression
                                  regularExpressionWithPattern:pattern
                                  options:0 error:nil];
    
    NSArray *matchResults = [regEx matchesInString:self options:0 range:area];
    
    NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:matchResults.count];
    
    for (NSTextCheckingResult *result in matchResults) {
        NSRange substrRange = NSMakeRange(pos, [result range].location-pos);
        [returnArray addObject:[self substringWithRange:substrRange]];
        pos = [result range].location + [result range].length;
    }
    
    if (pos < area.length) {
        [returnArray addObject:[self substringFromIndex:pos]];
    }
    
    return returnArray;
}

//计算字节的长度 中文字节算3个 英文算1个
- (int)calculateTextNumber
{
    float number = 0.0;
    int index;
    for (index=0; index < [self length]; index++) {
        
        NSString *character = [self substringWithRange:NSMakeRange(index, 1)];
        
        if ([character lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3) {
            number = number + 3;
        } else {
            number = number + 1;
        }
    }
    return ceil(number);
}
@end
